package com.zhangqie.dnui.demo1;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListPopupWindow;
import android.widget.PopupMenu;
import android.widget.Toast;

import com.zhangqie.dnui.R;
import com.zhangqie.dnui.databinding.DemoA1Binding;

/**
 * Created by zhangqie on 2018/8/7.
 * Describe:
 *
 * 1:SwipeRefreshLayout
 *
 * 2: PopupWindow(listPopupWindow,PopupMenu)
 *
 * 3：LinearLayoutCompat
 *
 */

public class Demo1_1 extends AppCompatActivity {


    DemoA1Binding demoA1Binding;

    private String[] strings = new String[]{"item1","item2","我的"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        demoA1Binding = DataBindingUtil.setContentView(this, R.layout.demo_a1);
        initView();
    }

    private void initView(){
        demoA1Binding.swipeRefresh.setSize(SwipeRefreshLayout.LARGE);
        demoA1Binding.swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(Demo1_1.this,"1",Toast.LENGTH_LONG).show();
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        demoA1Binding.tvData.setText("刷新后的数据");
                        demoA1Binding.swipeRefresh.setRefreshing(false);
                    }
                }, 2000);
            }
        });
        demoA1Binding.swipeRefresh.setColorSchemeColors(Color.RED,Color.BLUE,Color.GRAY);
        //设置进度条的背景颜色
        demoA1Binding.swipeRefresh.setProgressBackgroundColorSchemeColor(Color.YELLOW);
        //设置下拉多少距离开始刷新
        demoA1Binding.swipeRefresh.setDistanceToTriggerSync(70);



        //setRefreshing(boolean refreshing)

        //设置刷新状态，true表示正在刷新，false表示取消刷新。



    }


    /***
     * ListPopupWindow
     * @param view
     */
    public void showPopup(View view){
        final ListPopupWindow listPopupWindow = new ListPopupWindow(this);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strings);
        listPopupWindow.setAdapter(adapter);
        //设置锚点，弹出的位置相对于view
        listPopupWindow.setAnchorView(view);
        listPopupWindow.setWidth(500);
        listPopupWindow.setHeight(200);

        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),"点击了"+ strings[position],Toast.LENGTH_LONG).show();
                Log.i("aa","点击了"+strings[position]);
                listPopupWindow.dismiss();
            }
        });


        listPopupWindow.show();

    }


    public void showPopupMenu(View view){
        PopupMenu popupMenu = new PopupMenu(this,view);
        popupMenu.getMenuInflater().inflate(R.menu.mian1,popupMenu.getMenu());
        //popupMenu.setOnMenuItemClickListener(new On);
        popupMenu.show();
    }




}
